const puppeteer = require("puppeteer");
const cheerio = require("cheerio");

async function search(keyword){
    const browser = await puppeteer.launch({
        defaultViewport:null
    })
    const page = await browser.newPage()
    await page.goto(`https://music.163.com/#/search/m/?s=${keyword}&type=10`)
    var iframe = await page.$("#g_iframe")
    var frame = await iframe.contentFrame()
    var content = await frame.content()
    const $ = cheerio.load(content,{
        decodeEntities:false
    })
    var searchData = []
    var items = $(".n-srchrst ul li")
    items.each((index,value)=>{
        var coverImage = $(value).find(".u-cover img").attr("src")
        var title = $(value).find(".dec").text()
        var name = $(value).find(".nm a").text()
        searchData.push({
            coverImage,
            title,
            name
        })
    })
    return searchData
}
module.exports = search